﻿using DreamWork.Model;
using DreamWork.Model.ViewModel;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Threading.Tasks;

namespace DreamWork.WebAPI
{
    public abstract class DreamWorkControllerBase : Controller
    {
		protected string CurrentUserId
		{
			get
			{
				var currentUser = HttpContext.User;
				if (currentUser.HasClaim(x => x.Type == ClaimTypes.NameIdentifier))
				{
					return currentUser.Claims.FirstOrDefault(x => x.Type == ClaimTypes.NameIdentifier).Value;
				}
				throw new NullReferenceException("There is no valid current user.");
			}
		}

		protected OkObjectResult Success()
        {
            return Ok(new JsonResponseResult()
            {
                IsSuccess = true
            });
        }
        protected OkObjectResult Success(string content,string message = "")
        {
            return Ok(new DataJsonResult()
            {
                IsSuccess = true,
                Data = content,
                Message = message
            });
        }
        protected OkObjectResult Success(bool content, string message = "")
        {
            return Ok(new DataJsonResult()
            {
                IsSuccess = true,
                Data = content,
                Message = message
            });
        }
        protected OkObjectResult Success<T>(T data, string message = "") where T : ViewModelBase
        {
            return Ok(new DataJsonResult<T>()
            {
                IsSuccess = true,
                Data = data,
                MetaData = Activator.CreateInstance<T>().GetMetaData(),
                Message = message
            });
        }

        protected OkObjectResult Success<T>(IEnumerable<T> data, PageResultInfo pageInfo, string message="") where T : ViewModelBase
        {
            return Ok(new PagedJsonResult<T>()
            {
                IsSuccess = true,
                Data = data,
                PageInfo = pageInfo,
                MetaData = Activator.CreateInstance<T>().GetMetaData(),
                Message = message
            });
        }
        protected OkObjectResult Success<T>(IEnumerable<T> data,string message = "") where T : ViewModelBase
        {
            return Ok(new ListJsonResult<T>()
            {
                IsSuccess = true,
                Data = data,
                Message = message,
                MetaData = Activator.CreateInstance<T>().GetMetaData(),
            });
        }
        protected OkObjectResult Success<T>(List<T> data, string message = "") where T : ViewModelBase
        {
            return Ok(new ListJsonResult<T>()
            {
                IsSuccess = true,
                Data = data,
                Message = message,
                MetaData = Activator.CreateInstance<T>().GetMetaData(),
            });
        }
        protected OkObjectResult Success<T>(IList<T> data, string message = "") where T : ViewModelBase
        {
            return Ok(new ListJsonResult<T>()
            {
                IsSuccess = true,
                Data = data,
                Message = message,
                MetaData = Activator.CreateInstance<T>().GetMetaData(),
            });
        }
        protected OkObjectResult Error(string message)
        {
            return Ok(new DataJsonResult()
            {
                IsSuccess = false,
                Message = message
            });
        }
        protected OkObjectResult Error<T>(string message) where T : ViewModelBase
        {
            return Ok(new DataJsonResult<T>()
            {
                IsSuccess = false,
                Message = message
            });
        }
        protected OkObjectResult Error<T>(T data, string message) where T : ViewModelBase
        {
            return Ok(new DataJsonResult<T>()
            {
                IsSuccess = false,
                Data = data,
                MetaData = Activator.CreateInstance<T>().GetMetaData(),
                Message = message
            });
        }
    }
}
